package 单表查询;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo01 {
    public static void main(String[] args)  throws  Exception{
//        1.加载驱动com.mysql.jdbc.Driver
        Class.forName("com.mysql.jdbc.Driver");
//        2.使用驱动管理器(DriverManager)连接数据库
        String url = "jdbc:mysql://localhost:3306/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
        String user = "root";
        String pass="1234";
        Connection conn = DriverManager.getConnection(url, user, pass);
//        3书写sql语句，并执行
        String sql = "SELECT * from t_user";
//        4创建执行(Statement)sql的对象(Connection)
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
//        5.从rs中取出结果
        while (rs.next()){
            String username = rs.getString("username");
            String fullname = rs.getString("fullname");
            String password = rs.getString("password");
            String address = rs.getString("address");
            System.out.println(username+","+fullname+","+password+","+address);
        }
//        6.关闭连接
        rs.close();
        stmt.close();
        conn.close();
    }
}
